//
//  YFTabBarVC.m
//  Study
//
//  Created by duan on 2017/11/30.
//  Copyright © 2017年 fan. All rights reserved.
//

#import "YFTabBarVC.h"

#import "TMDNavigationController.h"

#import "HomeViewController.h"
#import "MessageViewController.h"
#import "PersonalViewController.h"


@interface YFTabBarVC ()

@end

@implementation YFTabBarVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
     //去掉tabbar的上面的线
     [self change];
    
     [self initTabBarController];//加载子控制器
    
}

-(void)initTabBarController
{

    HomeViewController *homeVC = [[HomeViewController alloc] init];
    homeVC.title = @"首页";
    [self setupChildVc:homeVC title:@"首页" image:@"home_normal" selectedImage:@"home_highlight"];

    MessageViewController *publishVC = [[MessageViewController alloc] init];
    publishVC.title = @"消息";
    [self setupChildVc:publishVC  title:@"消息" image:@"message_normal" selectedImage:@"message_highlight"];
    
    PersonalViewController  *personVC = [[PersonalViewController alloc] init];
    personVC.title = @"我的";
    [self setupChildVc:personVC  title:@"我的" image:@"account_normal" selectedImage:@"account_highlight"];
    
}
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    TMDNavigationController *nav = [[TMDNavigationController alloc]  initWithRootViewController:vc];
    [self addChildViewController:nav];
    nav.tabBarItem.title = title;
    nav.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [nav.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor colorWithWhite:51 / 255.0 alpha:1] forKey:NSForegroundColorAttributeName] forState:UIControlStateNormal];
    [nav.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor colorWithWhite:51 / 255.0 alpha:1] forKey:NSForegroundColorAttributeName] forState:UIControlStateSelected];
    
}

-(void)change{
    
      //去除tabbar上面的黑线
      self.tabBar.backgroundColor = [UIColor whiteColor];
      [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
      [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
    
      //在上面加上阴影
      [self dropShadowWithOffset:CGSizeMake(0, -0.5)
                          radius:1
                           color:[UIColor grayColor]
                         opacity:0.3];
}

- (void)dropShadowWithOffset:(CGSize)offset
                      radius:(CGFloat)radius
                       color:(UIColor *)color
                     opacity:(CGFloat)opacity {
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.tabBar.bounds);
    self.tabBar.layer.shadowPath = path;
    CGPathCloseSubpath(path);
    CGPathRelease(path);
    
    self.tabBar.layer.shadowColor = color.CGColor;
    self.tabBar.layer.shadowOffset = offset;
    self.tabBar.layer.shadowRadius = radius;
    self.tabBar.layer.shadowOpacity = opacity;
    
    // Default clipsToBounds is YES, will clip off the shadow, so we disable it.
    self.tabBar.clipsToBounds = NO;
}
@end
